/*-----------------------------------------------File Info------------------------------------------------
** File Name:               key.c  
** Last modified date:      2016.07.22
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.07
** author:                  YLY
** Version:                 V0.1
** Descriptions:            键盘扫描处理函数
**--------------------------------------------------------------------------------------------------------
*/
/*************************************************************
* 	include 
* 	头文件	
*************************************************************/
#include "key.h"
#include "mainTask.h"

const uint32_t swPperiph[8]={GPIOB,GPIOB,GPIOB,GPIOB,GPIOB,GPIOB,GPIOA,GPIOA};
const uint32_t swPin[8]={GPIO_PIN_12,GPIO_PIN_13,GPIO_PIN_14,GPIO_PIN_15,GPIO_PIN_8,GPIO_PIN_9,GPIO_PIN_8,GPIO_PIN_12};
static uint32_t gSwValue;
static uint8_t count[8];
static uint8_t value[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
	
uint32_t getSwValue(void)
{
	return gSwValue; 
}
static void keyScan(void)
{	


	uint8_t i,pin;
	
	for(i=0; i<8; i++)
	{
		pin = gpio_input_bit_get(swPperiph[i],swPin[i]);	
		if(value[i] != pin)	/* 值不同			*/
		{
			if(count[i] < 10)
				count[i]++;
			
			if(count[i] == 5)	/* 25,20mS*25次=500mS */
			{
				if(pin)
					gSwValue |= BIT(i);			//记录IO状态
				else
					gSwValue &= ~BIT(i);		//记录IO状态
				
				if(value[i] != 0xff)	//初始化IO状态
				{
					sendMsgToTask(gMainTaskId, MAIN_KEY_MSG, (void *)gSwValue, 4);						
				}
				value[i] = pin;		/* 更新状态*/
			}
		}
		else
		{
			count[i] = 0;
		}
	}
}

void keyInit(void)
{
	createTimer(20,keyScan);	//定时器扫描按键	
}